﻿// CInClientDlg.cpp: 实现文件
//

#include "pch.h"
#include "HotelManagementMFC.h"
#include "afxdialogex.h"
#include "CInClientDlg.h"


// CInClientDlg 对话框

IMPLEMENT_DYNAMIC(CInClientDlg, CDialog)

CInClientDlg::CInClientDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DLG_CLIENT_IN, pParent)
{

}

CInClientDlg::~CInClientDlg()
{
}

void CInClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_NUM, m_nNum);
	//  DDX_Control(pDX, IDC_NAME, m_nname);
	DDX_Control(pDX, IDC_NAME, m_nName);
	DDX_Control(pDX, IDC_SEX, m_nSex);
	DDX_Control(pDX, IDC_FROM, m_nFrom);
	DDX_Control(pDX, IDC_PAPERNUM, m_nPaperNum);
	DDX_Control(pDX, IDC_ROOMNUM, m_nRoomNum);
	//  DDX_Control(pDX, IDC_TIME, m_nTime);
	DDX_Control(pDX, IDC_PREPAYMENT, m_nPrepayment);
	DDX_Control(pDX, IDC_LIST_CLIENT_IN, m_listRoom);
	DDX_Control(pDX, IDC_TIME, m_dInDate);
}


BEGIN_MESSAGE_MAP(CInClientDlg, CDialog)
	ON_BN_CLICKED(IDOK, &CInClientDlg::OnSave)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_CLIENT_IN, &CInClientDlg::OnDblclkListClientIn)
END_MESSAGE_MAP()


// CInClientDlg 消息处理程序


BOOL CInClientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_listRoom.ModifyStyle(0, LVS_REPORT | LVS_SHOWSELALWAYS);
	m_listRoom.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);

	m_listRoom.SetBkColor(RGB(247, 247, 255));
	m_listRoom.SetTextBkColor(RGB(247, 247, 255));

	m_listRoom.InsertColumn(0, L"房间编号", LVCFMT_CENTER, 80);
	m_listRoom.InsertColumn(1, L"房间类型", LVCFMT_CENTER, 80);
	m_listRoom.InsertColumn(2, L"额定人数", LVCFMT_CENTER, 80);
	m_listRoom.InsertColumn(3, L"实际人数", LVCFMT_CENTER, 80);
	m_listRoom.InsertColumn(4, L"是否有电视", LVCFMT_CENTER, 100);
	m_listRoom.InsertColumn(5, L"是否有空调", LVCFMT_CENTER, 100);
	m_listRoom.InsertColumn(6, L"是否有卫生间", LVCFMT_CENTER, 120);
	m_listRoom.InsertColumn(7, L"备注", LVCFMT_CENTER, 300);

	// 连接数据库，获取数据集
	CString strSQL;
	int i = 0;
	_RecordsetPtr pRs;
	// 查询空房和未满房间
	strSQL.Format(L"Select RoomCode, RoomType, PeopleNum, FactPeopleNum, TV, AirCondition, Toilet, Remark From Room Where FactPeopleNum = 0 Or PeopleNum - FactPeopleNum > 0");

	try {
		m_pRecordSet_1.CreateInstance(__uuidof(Recordset));
		m_pRecordSet_1->Open(strSQL.AllocSysString(),
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);

		_variant_t Holder;
		CString tmp;
		if (!m_pRecordSet_1->GetRecordCount() == 0)
		{
			while (!m_pRecordSet_1->adoEOF)
			{
				theApp.m_GetCollect(m_pRecordSet_1, Holder, tmp, "RoomCode");
				m_listRoom.InsertItem(i, tmp);

				theApp.m_GetCollect(m_pRecordSet_1, Holder, tmp, "RoomType");
				{
					CString strSubSQL;
					strSubSQL.Format(L"Select RoomTypeName From RoomType Where RoomTypeNo = '%s'", tmp);
					pRs.CreateInstance(__uuidof(Recordset));
					pRs->Open(strSubSQL.AllocSysString(), theApp.m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
					if (!pRs->GetRecordCount() == 0)
						theApp.m_GetCollect(pRs, Holder, tmp, "RoomTypeName");
					pRs->Close();
				}
				m_listRoom.SetItemText(i, 1, tmp);

				theApp.m_GetCollect(m_pRecordSet_1, Holder, tmp, "PeopleNum");
				m_listRoom.SetItemText(i, 2, tmp);

				theApp.m_GetCollect(m_pRecordSet_1, Holder, tmp, "FactPeopleNum");
				m_listRoom.SetItemText(i, 3, tmp);

				theApp.m_GetCollect(m_pRecordSet_1, Holder, tmp, "TV");
				if (tmp.CompareNoCase(L"1") == 0)
					m_listRoom.SetItemText(i, 4, L"有");
				else
					m_listRoom.SetItemText(i, 4, L"无");

				theApp.m_GetCollect(m_pRecordSet_1, Holder, tmp, "AirCondition");
				if (tmp.CompareNoCase(L"1") == 0)
					m_listRoom.SetItemText(i, 5, L"有");
				else
					m_listRoom.SetItemText(i, 5, L"无");

				theApp.m_GetCollect(m_pRecordSet_1, Holder, tmp, "Toilet");
				if (tmp.CompareNoCase(L"1") == 0)
					m_listRoom.SetItemText(i, 6, L"有");
				else
					m_listRoom.SetItemText(i, 6, L"无");

				theApp.m_GetCollect(m_pRecordSet_1, Holder, tmp, "Remark");
				m_listRoom.SetItemText(i, 7, tmp);

				m_pRecordSet_1->MoveNext();
				i++;
			}
		}
		else
		{
			m_listRoom.InsertItem(1, L"- NULL -");
			AfxMessageBox(L"未查询到房间信息！", MB_OK);
		}
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void CInClientDlg::OnSave()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strRoomNo;
	m_nRoomNum.GetWindowText(strRoomNo);
	if (strRoomNo.IsEmpty())
	{
		AfxMessageBox(L"你还没有选择房间！", MB_OK);
		return;
	}

	// 获取入住时间
	COleDateTime dtChosenTime;
	m_dInDate.GetTime(dtChosenTime);
	CString strDate;
	strDate.Format(L"%d-%d-%d", dtChosenTime.GetYear(), dtChosenTime.GetMonth(), dtChosenTime.GetDay());

	CString strNo, strName, strSex, strFrom, strPaperNum, strDeposit;
	m_nNum.GetWindowText(strNo);
	m_nName.GetWindowText(strName);
	m_nSex.GetWindowText(strSex);
	m_nFrom.GetWindowText(strFrom);
	m_nPaperNum.GetWindowText(strPaperNum);
	m_nPrepayment.GetWindowText(strDeposit);

	// 插入数据
	CString strSQL;
	strSQL.Format(L"Insert into Client(ClientNo, ClientName, Sex, NativePlace, PaperNum, InDate, RoomNo, Deposit) Values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d')", strNo, strName, strSex, strFrom, strPaperNum, strDate, strRoomNo, _ttoi(strDeposit));

	// 更新房间人数
	CString strSubSQL;
	strSubSQL.Format(L"Update Room Set FactPeopleNum = FactPeopleNum + 1 Where RoomCode = '%s'", strRoomNo);

	try {
		HRESULT hr;
		hr = theApp.m_pConnection->Execute(strSQL.AllocSysString(), NULL, adCmdText);
		if (SUCCEEDED(hr))
		{
			AfxMessageBox(L"客户入住成功！", MB_OK);
			theApp.m_pConnection->Execute(strSubSQL.AllocSysString(), NULL, adCmdText);
			OnInitDialog();
		}
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}

	//CDialog::OnOK();
}


void CInClientDlg::OnDblclkListClientIn(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nItem = pNMItemActivate->iItem;
	if (nItem >= 0 && nItem < m_listRoom.GetItemCount())
	{
		m_nRoomNum.SetWindowText(m_listRoom.GetItemText(nItem, 0));
	}

	*pResult = 0;
}
